<?php
    get_header();
    
    global $post,$wpdb;
    
    $store_id = $post->ID;
    $store_mask_link = get_post_meta($store_id, 'store_mask_metadata', true);
    $store_name = $post->post_title;
    $store_des = $post->post_content;
    $store_logo = get_post_meta($store_id, 'logo_metadata', true);
    $today = date('Y/m/d');
    $row_display = get_option('number_row_store_single');
    $display = $row_display*4;
    if($row_display == ''){
        $display = 16;
    }
    $not_in = get_option('sticky_posts');
    $check_store = get_post_meta($not_in[0],'store_id_metadata',true);
    $args = array(
        'post_type' => 'post'
        ,'post_status' =>'publish'
        ,'posts_per_page' => -1
        ,'meta_query' =>array(
            array(
                'key' => 'store_id_metadata',
                'value' => $store_id,
                'compare' => '='
            )
            ,array(
                'key' => 'start_date_metadata',
                    'value' => $today,
                    'compare' => '<='
            )
            ,array(
                'key' => 'end_date_metadata',
                    'value' => $today,
                    'compare' => '>='
            )
        )
    );
    if($store_id != $check_store){
        $args['post__not_in'] = get_option('sticky_posts');
    }
    $query = new WP_Query($args);
    $post = $query->posts;
    $total_post = $query->found_posts;    
?>
<div id="all-deals">
            <?php  
            if(current_user_can('edit_post', $store_id))
            {
                echo edit_post_link('Edit Store','<a style="margin-left:26px">','</a>',$store_id);
            } 
            ?>
        <div class="all-deals-list row-fluid">        
            <div class="row-fluid store-search single-deal">            
                <div class="image-store span2">
                    <div class="imgx">
                    <a href="<?php echo $store_mask_link; ?>" title="<?php echo 'View '.$store_name ;?>" target="_blank">
                        <img src="<?php echo $store_logo; ?>" alt="<?php echo $store_name; ?>" title="<?php echo $store_name; ?>" />
                    </a>
                    </div>
                </div>
                <div class="description-store span10">
                    <h1><?php echo $store_name; ?>: </h1>
                    <p><?php echo $store_des; ?></p>
                </div>
            </div>
            <hr class="single-store-hr"/>
            <div class="all-deals-list row-fluid">
                <?php
                    if($query->have_posts()){ ?>
                <p class="top-deal-store"><?php echo $store_name;?> top deals</p>
                        <?php 
                            $arr = array();
                            foreach ($query->posts as $p) {
                                array_push($arr, $p->ID);
                            }
                            pagination_jquery($arr,1,0,0,$display);
                    }else{ 
                            $args = array(
                                'post_type' => 'post',
                                'post_status' =>'publish',
                                'posts_per_page' => 20,
                                'post__not_in' => get_option('sticky_posts'),
                                'meta_query' =>array(
                                    array(
                                        'key' => 'start_date_metadata',
                                            'value' => $today,
                                            'compare' => '<='
                                    ),
                                    array(
                                        'key' => 'end_date_metadata',
                                            'value' => $today,
                                            'compare' => '>='
                                    )
                                )
                            );
                            $query = new WP_Query($args);
                            $post = $query->posts;
                        ?>
                        <div class="store-empty row-fluid">
                            <span>We are really sorry, there is no deal in store you are visitting!</span>
                            <p>Please go <a href="<?php echo site_url();?>"> FindsDeals.com</a>  to see a lot of the lastest deals, or we already have some suggestions below.</p>
                        </div>
                            <?php 
                                foreach ($post as $p){
                                    cpx_get_deal_info($p->ID);
                                }
                            ?>
                    <?php }
                ?>
            </div>
            
            
        </div>
</div>
<?php get_footer(); ?>